<?php
/*
 * 广告预订
 * @arrange Mark
 * @package ixdcw
 * @file    ad.inc.php
 * @time    12-12-19 上午10:48
 */
defined('IN_IXDCW') or exit('Access Denied');
login();
require DT_CORE.'/'.$module.'/common.inc.php';
require_once DT_INC.'/post.func.php';
$MG['ad'] or Prompt::alert(lang('message->without_permission_and_upgrade'), 'goback');
include load('extend.lang');

$adurl = SITE_URL.'ad/';
$TYPE = $L['ad_type'];

$do = new AdModule();

if($action == 'add2') {
	$pid = isset($pid) ? intval($pid) : 0;
	if($pid) {
		$EXT['ad_buy'] or dheader($MOD['linkurl'].'ad.php');
		$currency = $EXT['ad_currency'];
		$unit = $currency == 'money' ? $DT['money_unit'] : $DT['credit_unit'];
		$p = $db->getOne("SELECT * FROM {$db->getPre()}ad_place WHERE pid=$pid");
		$p or Prompt::message($L['not_ad_place'], $adurl);
		$months = array(1, 2, 3, 6, 12, 24);
		$t = $db->getOne("SELECT MAX(totime) AS totime FROM {$db->getPre()}ad WHERE pid=$pid AND totime>$DT_TIME");
		$fromtime = $t['totime'] ? $t['totime'] : $DT_TIME + 86400;
		$fromdate = timetodate($fromtime, 3);
		$typeid = $p['typeid'];
		$price = $p['price'];
		if($submit) {
			(is_date($post['fromtime'])) or Prompt::message($L['pass_ad_from']);
			in_array($month, $months) or Prompt::message($L['pass_ad_month']);
			$amount = $price*$month;
			if($amount) {
				if($currency == 'money') {
					if($amount > $_money) Prompt::message($L['money_not_enough'], $MODULE[2]['linkurl'].'charge.php?action=pay&amount='.($amount-$_money));
					is_payword($_username, $password) or Prompt::message($L['error_payword']);
				} else {
					if($amount > $_credit) Prompt::message($L['credit_not_enough'], $MODULE[2]['linkurl'].'credit.php?action=buy&amount='.($amount-$_credit));
				}
			}
			$ad = array();
			$ad['image_src'] = $ad['flash_src'] = $ad['code'] = '';
			if($typeid == 1 || $typeid == 7) {
				if(strlen($post['code']) < 10) Prompt::message($L['pass_ad_code']);
				$ad['code'] = $post['code'];
			} else if($typeid == 2) {
				if(strlen($post['text_name']) < 2) Prompt::message($L['pass_ad_text_name']);
				if(strlen($post['text_url']) < 10) Prompt::message($L['pass_ad_text_url']);
				$ad['text_name'] = $post['text_name'];
				$ad['text_url'] = fix_link($post['text_url']);
				$ad['text_title'] = $post['text_title'];
			} else if($typeid == 3 || $typeid == 5) {
				if(strlen($post['image_src']) < 15) Prompt::message($L['pass_ad_image_src']);
				$ad['image_src'] = $post['image_src'];
				$ad['image_url'] = fix_link($post['image_url']);
				$ad['image_alt'] = $post['image_alt'];
			} else if($typeid == 4) {
				if(strlen($post['flash_src']) < 15 || strpos($post['flash_src'], '.swf') === false) Prompt::message($L['pass_ad_flash_src']);
				$ad['flash_src'] = $post['flash_src'];
				$ad['flash_loop'] = $post['flash_loop'] ? 1 : 0;
				$ad['flash_url'] = fix_link($post['flash_url']);
			} else if($typeid == 6) {
				$post['keyid'] = intval($post['keyid']);
				$post['key_id'] or Prompt::message($L['pass_ad_infoid']);
				$ad['key_id'] = $post['keyid'];
			}
			if($typeid == 6 || $typeid == 7) {
				$ad['key_moduleid'] = $p['moduleid'];
				$ad['key_catid'] = $post['catid'];
				$ad['key_word'] = $post['word'];
			}
			$ad['fromtime'] = strtotime($post['fromtime']);
			$ad['totime'] = strtotime($post['fromtime']) + 86400*30*$month;
			$ad['pid'] = $pid;
			$ad['typeid'] = $typeid;
			$ad['amount'] = $amount;
			$ad['currency'] = $currency;
			$ad['title'] = $post['fromtime'].'('.$_username.')';
			$ad['introduce'] = timetodate($DT_TIME, 5).' '.$L['ad_buy_paid'].$amount.$unit;
			$ad['note'] = $L['ad_buy_note'].'('.$DT_IP.')';
			$ad['descs'] = $post['descs'];
			$ad['status'] = 2;
			$ad['username'] = $_username;
			if($amount) {
				if($currency == 'money') {
					money_add($_username, -$amount);
					money_record($_username, -$amount, $L['in_site'], 'system', $L['pay_in_site'], $p['name'].$L['ad_buy_title'].$month.$L['month']);
				} else {
					credit_add($_username, -$amount);
					credit_record($_username, -$amount, 'system', $p['name'].$L['ad_buy_title'], $month.$L['month']);
				}
			}
			$sqlk = $sqlv = '';
			foreach($ad as $k=>$v) {
				$sqlk .= ','.$k; $sqlv .= ",'$v'";
			}
			$sqlk = substr($sqlk, 1);
			$sqlv = substr($sqlv, 1);
			$db->query("INSERT INTO {$db->getPre()}ad ($sqlk) VALUES ($sqlv)");
			$db->query("UPDATE {$db->getPre()}ad_place SET ads=ads+1 WHERE pid=$pid");
			Prompt::message($L['ad_buy_success'], '?status=3');
		}
	} else {
		dheader($adurl);
	}

	$head_title = $L['ad_buy_title'];
	include template('ad2', $module);
} elseif ($action == 'add') {

	if($submit) {

		if($post['pid']){
			$do->setPid($post['pid']);
			$p = $do->getOnePlace();
		}else{
			Prompt::alert(lang('message->without_pid'), 'goback');
		}
		$post['time'] = intval($post['time'])>0 ? intval($post['time']) : 0;

		if(!$post['time']){
			Prompt::alert(lang('message->not_true_time'), 'goback');
		}
		$timeType = array("1"=>"month","2"=>"year");
		if(!isset($timeType[$post['timetype']])){
			Prompt::alert(lang('message->not_true_timetype'), 'goback');
		}

		if($post['site']=='1'){
			$post["cityid"]= array("0" => "0");
			/*
			if($p["nation_price"]){
				$moneyBase = intval($p["nation_price"]);
			}else{
				Prompt::alert(lang('message->without_site'), 'goback');
			}
*/
			//Prompt::alert(lang('message->without_site'), 'goback');
		}elseif($post['site']=='2'){
			if(!$post["cityid"] || !is_array($post["cityid"])){
				Prompt::alert(lang('message->without_city'), 'goback');
			}
		}else{
			Prompt::alert(lang('message->without_site'), 'goback');
		}

		$post['pid'] = isset($post['pid']) ? intval($post['pid']) : 0;
		if($post['pid']){
			$do->setPid($post['pid']);
			$p = $do->getOnePlace();
		}else{
			Prompt::alert(lang('message->without_pid'), 'goback');
		}

		$pay = array();
		$pay["pid"] = $post['pid'];
		$pay["username"] = $_username;
		$pay["starttime"] = timetodate($DT_TIME, 3);
		$pay["overtime"] = timetodate(strtotime("+".intval($post['time'])." ".$timeType[$post['timetype']],$DT_TIME), 3);
		$pay["areaids"] = $post["cityid"];


		if($do->isPay($pay)) {
			$do->addPay($pay);
			Prompt::message($L['ad_buy_success'], '?status=3');
		} else {
			Prompt::alert($do->getErrmsg(), 'goback');
		}

	}else{
		$pid = isset($pid) ? intval($pid) : 0;
		$ads = $do->getPlace('(placeT.nation_price<>0 OR placeT.subnation_price<>0)');

		if($pid){
			$do->setPid($pid);
			$p = $do->getOnePlace();
		}
		//如果广告位存在 导出
		if($p){
			//此广告位主站是否已被购买
			$placeHasSationMain = $do->getAdByPidCityid($pid,0) ? 1:0;
			$placeResidueCity = $do->getPlaceResidueCity($pid);
		}
		//如果有可购买分站 导出省份列表
		if($placeResidueCity && $p['subnation_price']){
			$placeResidueProvince = array();
			foreach($placeResidueCity as $areaids){
				if($AREA[$areaids["areaid"]]["parentid"]!=0){
					if($AREA[$areaids["areaid"]]["parentid"]==3421){
						$placeResidueProvince[$areaids["areaid"]] = $AREA[$areaids["areaid"]];
					}else{
						$placeResidueProvince[$AREA[$areaids["areaid"]]["parentid"]] = $AREA[$AREA[$areaids["areaid"]]["parentid"]];
					}
				}
			}
		}

		$head_title = $L['ad_buy_title'];
		include template('ad_buy', $module);
	}
} elseif ($action == 'edit') {
	$payInfo = $do->getUserById($payid);
	$payInfo['userid'] == $_userid or Prompt::alert(lang('message->without_right_to_this'), 'goback');

	if ($submit){

		$sql = "update {$db->getPre()}ad set image_src='{$thumb}',descs='{$descs}', image_url='{$image_url}', status=2 where aid={$aid} and username='{$_username}' ";
		if ($db->query($sql)) {
			dmsg('更新成功', '');
		}
	}else{
		$do->setPid($payInfo['pid']);
		$p = $do->getOnePlace();
		$p or Prompt::alert(lang('message->nothave_pid'), 'goback');;
		$p['typename'] =$do->getTypeName($p['catid']);
		if($payInfo['areaids']=='0'){
			$p['type']='总站';
			$areaids = array("0" => "0");
		}else{
			$p['type']='分站';
			$areaids = explode(',',$payInfo['areaids']);
		}

		foreach($areaids as $areaid){
			$adChild = $do->getAdByPidCityid($p['pid'],$areaid);
			if($adChild){

			}else{

			}
		}
		$head_title = $L['ad_buy_title'];
		include template('ad_edit', $module);
	}



} else {
	$adPagesize = 5;
	$adOffset        = ( $page - 1 )*$adPagesize;
	$condition = "userT.userid='$_userid'";
	$r = $db->getOne("SELECT COUNT(*) AS num FROM {$db->getPre()}ad_user userT WHERE $condition");
	$pages = pages($r['num'], $page, $adPagesize);
	$lists = array();
	$result = $db->query("SELECT userT.*,placeT.`name`,typeT.typename FROM {$db->getPre()}ad_user userT LEFT JOIN {$db->getPre()}ad_place placeT ON userT.pid=placeT.pid LEFT JOIN {$db->getPre()}type typeT ON placeT.catid=typeT.typeid WHERE $condition  ORDER BY userT.overtime DESC  LIMIT $adOffset,$adPagesize");
	while($r = $db->fetchArray($result)) {
		if($r['overtime'] < $DT_TIME) {
			$r['process'] = 2;
		} else if($r['starttime'] > $DT_TIME) {
			$r['process'] = 0;
		} else {
			$r['process'] = 1;
		}
		$r['days'] = $r['overtime'] > $DT_TIME ? intval(($r['overtime']-$DT_TIME)/86400) : 0;
		if($r['areaids']=='0'){
			$r['type']='总站';
		}else{
			$r['type']='分站';
			$r['areaids']=explode(',',$r['areaids']);
		}
		$lists[] = $r;
	}

	$head_title = $L['ad_buy_title'];
	include template('ad', $module);
}


?>